package com.longtop.leaning.dao;

import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

import com.longtop.leaning.data.AppData;
import com.longtop.leaning.entity.FlightData;
import com.longtop.leaning.util.StringUtils;

public class FltHTTPDao implements FltDao{

	private static final FltHTTPDao instance = new FltHTTPDao();

	private FltHTTPDao() {
	}

	public static FltHTTPDao getInstance() {
		return instance;
	}
	
	public void test() {
		
		Vector flights = FltHTTPDao.getInstance().findFlts("", null, null, -1);
		Enumeration e = flights.elements();
		while(e.hasMoreElements()){
			FlightData flight = (FlightData)e.nextElement();
			System.out.println(flight.getFltno());
		}
		
	}
	
	public void addFlts(Vector flts) {
		throw new RuntimeException("no supported operation!");
	}

	public Vector findFlts(String fltno, Date fltdateStart, Date fltdateEnd, int fltstatus) {
		Vector flights = new Vector();
		String params = "";
		if(!StringUtils.isEmpty(fltno)){
			params +="fltno="+fltno+"&";
		}
		if(fltdateStart != null){
			params +="fltdate1="+fltdateStart.toString()+"&";
		}
		if(fltdateEnd != null){
			params +="fltdate2="+fltdateEnd.toString()+"&";
		}
		if(fltstatus > -1){
			params +="fltstatus="+fltstatus+"&";
		}
		
		String url = AppData.SERVER_URL+"flt_query.do?"+params;
		Vector flightStrings = XMLUtil.xml2StringVector(XMLUtil.getContentFromURL(url),AppData.FLIGHT_TAG);
		
		Enumeration e = flightStrings.elements();
		FlightData flight = null;
		while(e.hasMoreElements()){
			flight = (FlightData)FlightData.decode((String)e.nextElement());
			flights.addElement(flight);
		}
		
		return flights;
	}
	
	public void clearFlt() {
		throw new RuntimeException("no supported operation!");
	}

}
